﻿using System;
using System.Configuration;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KPIS.DBM;
using KPIS.GERP.GHRMS.MODEL;
using KPIS.GERP.GHRMS.IDAL;

namespace KPIS.GERP.GHRMS.DAL
{
    public sealed class ClassDAL : IClassDAL
    {
        IDBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);

        public ClassDAL()
        {

        }
        public DataSet LoadClasses(ClassInfo cInfo)
        {
            string strSQL;
            int PosTypeSeq = cInfo.PositionTypes == null ? 0 : cInfo.PositionTypes.Sequence;
            int EmpTypeSeq = cInfo.EmployeeTypes == null ? 0 : cInfo.EmployeeTypes.Sequence;

            DataSet ds = null;

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(4);

                dbManager.AddParameters(0, "@Sequence", cInfo.Sequence);
                dbManager.AddParameters(1, "@PositionTypes", PosTypeSeq);
                dbManager.AddParameters(2, "@EmployeeTypes", EmpTypeSeq);
                dbManager.AddParameters(3, "@ActiveRecord", SystemInfo._ActiveRecordStatus);

                strSQL = "SELECT c.CLASS_SEQ, c.CLASS_NAME, c.CLASS_NAME_SHORT, c.CLASS_ENG, c.CLASS_ENG_SHORT"
                        + ", c.REMARKS, c.CREATED_BY, c.UPDATED_BY, c.POS_TYPE_SEQ, c.EMP_TYPE_SEQ"
                        + ", et.EMP_TYPE_NAME, pt.POS_TYPE_NAME"
                    + " FROM HRM_MP_CLASSES c"
                        + " LEFT JOIN HRM_PS_EMPLOYEE_TYPES et ON c.EMP_TYPE_SEQ = et.EMP_TYPE_SEQ"
                        + " LEFT JOIN HRM_MP_POSITION_TYPES pt ON c.POS_TYPE_SEQ = pt.POS_TYPE_SEQ"
                    + " WHERE c.RECORD_STATUS = @ActiveRecord"
                    + (cInfo.Sequence > 0 ? " AND c.CLASS_SEQ = @Sequence" : "")
                    + (PosTypeSeq > 0 ? " AND c.POS_TYPE_SEQ = @PositionTypes" : "")
                    + (EmpTypeSeq > 0 ? " AND c.EMP_TYPE_SEQ = @EmployeeTypes" : "");

                ds = dbManager.ExecuteDataSet(CommandType.Text, strSQL);                

            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.CloseReader();
                dbManager.Dispose();
            }

            return ds;
        }
        public DataSet LoadClassList(Nullable<int> Sequence, Nullable<int> EmployeeType, Nullable<int> PosSeq, Nullable<int> FieldSeq, Nullable<int> ClassSpecSeq)
        {
            string strSQL;

            DataSet ds = null;

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(6);

                dbManager.AddParameters(0, "@Sequence", Sequence);
                dbManager.AddParameters(1, "@EmployeeType", EmployeeType);
                dbManager.AddParameters(2, "@PosSeq", PosSeq);
                dbManager.AddParameters(3, "@FieldSeq", FieldSeq);
                dbManager.AddParameters(4, "@ClassSpecSeq", ClassSpecSeq);
                dbManager.AddParameters(5, "@ActiveRecord", SystemInfo._ActiveRecordStatus);

                strSQL = "SELECT c.CLASS_SEQ, c.CLASS_NAME, c.CLASS_NAME_SHORT, c.CLASS_ENG, c.CLASS_ENG_SHORT"
                        + ", c.POS_TYPE_SEQ, c.EMP_TYPE_SEQ, c.REMARKS, c.CREATED_BY, c.UPDATED_BY" 
                        + (EmployeeType == 1 ? ", d.SAL_MIN, d.SAL_MAX" : "")
                    + " FROM HRM_MP_CLASSES c"
                    + (EmployeeType == 1 ? " LEFT JOIN HRM_PR_SALARY_GROUPS g ON c.CLASS_SEQ = g.CLASS_SEQ" : "")
                    + (EmployeeType == 1 ? " LEFT JOIN HRM_PR_SALARY_GROUP_DTLS d ON g.SAL_GRP_SEQ = d.SAL_GRP_SEQ" : "")
                    + (FieldSeq.HasValue || PosSeq.HasValue ? " INNER JOIN HRM_MP_CLASS_SPEC_DTLS sd ON c.CLASS_SEQ = sd.CLASS_SEQ" : "")
                    + (FieldSeq.HasValue || PosSeq.HasValue ? " INNER JOIN HRM_MP_CLASS_SPECIFICATIONS cs ON sd.CLASS_SPEC_SEQ = cs.CLASS_SPEC_SEQ" :"")
                    + (FieldSeq.HasValue ? " LEFT JOIN HRM_MP_FIELD_POSITIONS f ON cs.CLASS_SPEC_SEQ = f.CLASS_SPEC_SEQ" : "")
                    + (PosSeq.HasValue ? " LEFT JOIN HRM_MP_POSITIONS p ON cs.CLASS_SPEC_SEQ = p.CLASS_SPEC_SEQ" : "")
                    + " WHERE c.RECORD_STATUS = @ActiveRecord"
                    + (EmployeeType.HasValue ? " AND c.EMP_TYPE_SEQ = @EmployeeType" : "")
                    + (Sequence.HasValue ? " AND c.CLASS_SEQ = @Sequence" : "")
                    + (PosSeq.HasValue ? " AND p.POS_SEQ = @PosSeq" : "")
                    + (FieldSeq.HasValue ? " AND f.FIELD_POS_SEQ = @FieldSeq" : "")
                    + (ClassSpecSeq.HasValue ? " AND cs.CLASS_SPEC_SEQ = @ClassSpecSeq" : "");

                ds = dbManager.ExecuteDataSet(CommandType.Text, strSQL);

            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.CloseReader();
                dbManager.Dispose();
            }

            return ds;
        }
        public ClassInfo GetClassInfo(int Sequence)
        {
            ClassInfo Info = new ClassInfo();
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(2);

                dbManager.AddParameters(0, "@DeleteRecord", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@Sequence", Sequence);

                strSQL = "SELECT CLASS_SEQ, CLASS_NAME"
                     + " FROM HRM_MP_CLASSES"
                     + " WHERE CLASS_SEQ = @Sequence";

                IDataReader rdr = dbManager.ExecuteReader(CommandType.Text, strSQL);

                while (rdr.Read())
                {
                    Info.Sequence = rdr.GetInt32(0);
                    Info.Name = rdr.IsDBNull(1) ? "" : rdr.GetString(1);
                }

                return Info;
            }
            catch
            {
                return null;
                throw;
            }
            finally
            {
                dbManager.CloseReader();
                dbManager.Dispose();
            }
        }
        public IList<ClassInfo> ListClasses(ClassInfo cInfo)
        {
            string strSQL;
            int PosTypeSeq = cInfo.PositionTypes == null ? 0 : cInfo.PositionTypes.Sequence;

            List<ClassInfo> infoList = new List<ClassInfo>();

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(3);

                dbManager.AddParameters(0, "@Sequence", cInfo.Sequence);
                dbManager.AddParameters(1, "@PositionTypes", PosTypeSeq);
                dbManager.AddParameters(2, "@DeleteRecord", SystemInfo._DeleteRecordStatus);

                strSQL = "SELECT CLASS_SEQ, CLASS_NAME, CLASS_NAME_SHORT, CLASS_ENG, CLASS_ENG_SHORT"
                        + ", REMARKS, CREATED_BY, UPDATED_BY, POS_TYPE_SEQ, EMP_TYPE_SEQ"
                    + " FROM HRM_MP_CLASSES"
                    + " WHERE RECORD_STATUS not like @DeleteRecord"
                    + (cInfo.Sequence > 0 ? " AND CLASS_SEQ = @Sequence" : "")
                    + (PosTypeSeq > 0 ? " AND POS_TYPE_SEQ = @PositionTypes" : "");

                IDataReader rdr = dbManager.ExecuteReader(CommandType.Text, strSQL);

                while (rdr.Read())
                {
                    EmployeeTypeDAL emptypeDAL = new EmployeeTypeDAL();
                    EmployeeTypeInfo etInfo = new EmployeeTypeInfo();
                    if (!rdr.IsDBNull(9))
                    {
                        IList<EmployeeTypeInfo> emptypeList = emptypeDAL.ListEmployeeTypes(new EmployeeTypeInfo(rdr.GetInt32(9)));
                        etInfo = emptypeList[0];
                    }

                    PositionTypeDAL postypeDAL = new PositionTypeDAL();
                    PositionTypeInfo ptInfo = new PositionTypeInfo();
                    if (!rdr.IsDBNull(8))
                    {
                        int posSeq = rdr.GetInt32(8);
                        IList<PositionTypeInfo> postypeList = postypeDAL.ListPositionTypes(posSeq, null);
                        if (postypeList.Count > 0) ptInfo = postypeList[0]; 
                    }
                    ClassInfo Info = new ClassInfo(rdr.GetInt32(0)
                        , rdr.IsDBNull(1) ? "" : rdr.GetString(1)
                        , rdr.IsDBNull(2) ? "" : rdr.GetString(2)
                        , rdr.IsDBNull(3) ? "" : rdr.GetString(3)
                        , rdr.IsDBNull(4) ? "" : rdr.GetString(4)
                        , rdr.IsDBNull(5) ? "" : rdr.GetString(5)
                        , rdr.IsDBNull(6) ? (int?)null : rdr.GetInt32(6)
                        , rdr.IsDBNull(7) ? (int?)null : rdr.GetInt32(7)
                        , ptInfo
                        , etInfo
                        );

                    infoList.Add(Info);
                }

            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.CloseReader();
                dbManager.Dispose();
            }

            return infoList;
        }
        
        //public IList<ClassInfo> LoadGridClasses(Nullable<int> PosTypeSeq)
        //{
        //    string strSQL;
        //    DataSet ds = null;
        //    DataRowCollection dr = null;
        //    IList<ClassInfo> infoList = new List<ClassInfo>();

        //    try
        //    {
        //        dbManager.Open();
        //        dbManager.CreateParameters(2);

        //        dbManager.AddParameters(0, "@PosTypeSeq", PosTypeSeq);
        //        dbManager.AddParameters(1, "@DeleteRecord", SystemInfo._DeleteRecordStatus);

        //        strSQL = "SELECT c.CLASS_SEQ, c.POS_TYPE_SEQ, c.EMP_TYPE_SEQ, c.CLASS_NAME, c.CLASS_NAME_SHORT"
        //                + ", pt.POS_TYPE_NAME"
        //             + " FROM HRM_MP_CLASSES c"
        //                + " LEFT JOIN HRM_MP_POSITION_TYPES pt ON c.POS_TYPE_SEQ = pt.POS_TYPE_SEQ"
        //             + " WHERE c.RECORD_STATUS not like @DeleteRecord"
        //             + (PosTypeSeq.HasValue ? " AND c.POS_TYPE_SEQ = @PosTypeSeq" : "") ;

        //        ds = dbManager.ExecuteDataSet(CommandType.Text, strSQL);
        //        dr = ds.Tables[0].Rows;
        //        if (dr.Count > 0)
        //        {
        //            for (int i = 0; i < dr.Count; i++)
        //            {
        //                ClassInfo info = new ClassInfo();
        //                info.ClassSeq = Convert.IsDBNull(dr[i]["CLASS_SEQ"]) ? (int?)null : Int32.Parse(dr[i]["CLASS_SEQ"].ToString());
        //                info.PositionTypeSeq = Convert.IsDBNull(dr[i]["POS_TYPE_SEQ"]) ? (int?)null : Int32.Parse(dr[i]["POS_TYPE_SEQ"].ToString());
        //                info.EmployeeTypeSeq = Convert.IsDBNull(dr[i]["EMP_TYPE_SEQ"]) ? (int?)null : Int32.Parse(dr[i]["EMP_TYPE_SEQ"].ToString());                        
        //                info.ClassName = Convert.IsDBNull(dr[i]["CLASS_NAME"]) ? "" : (dr[i]["CLASS_NAME"]).ToString();
        //                info.ClassNameShort = Convert.IsDBNull(dr[i]["CLASS_NAME_SHORT"]) ? "" : (dr[i]["CLASS_NAME_SHORT"]).ToString();
        //                info.PositionTypeName = Convert.IsDBNull(dr[i]["POS_TYPE_NAME"]) ? "" : (dr[i]["POS_TYPE_NAME"]).ToString();
        //                infoList.Add(info);
        //            }
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        throw (ex);
        //    }
        //    finally
        //    {
        //        dbManager.Dispose();
        //    }

        //    return infoList;
        //}

        public void Insert(ClassInfo Info)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(10);

                dbManager.AddParameters(0, "@PosTypeSeq", Info.PositionTypes == null ? (int?)null : Info.PositionTypes.Sequence);
                dbManager.AddParameters(1, "@EmpTypeSeq", Info.EmployeeTypes == null ? (int?)null : Info.EmployeeTypes.Sequence);
                dbManager.AddParameters(2, "@ClassName", Info.Name);
                dbManager.AddParameters(3, "@ClassNameShort", Info.NameShort);
                dbManager.AddParameters(4, "@ClassNameEng", Info.NameEng);
                dbManager.AddParameters(5, "@ClassNameEngShort", Info.NameEngShort);
                dbManager.AddParameters(6, "@Remarks", Info.Remarks);
                dbManager.AddParameters(7, "@RecordStatus", SystemInfo._ActiveRecordStatus);
                dbManager.AddParameters(8, "@CreatedBy", Info.CreatedBy);
                dbManager.AddParameters(9, "@CreatedWhen", DateTime.Today);

                strSQL = "INSERT INTO HRM_MP_CLASSES(POS_TYPE_SEQ, EMP_TYPE_SEQ, CLASS_NAME"
                        + ", CLASS_NAME_SHORT, CLASS_ENG, CLASS_ENG_SHORT"
                        + ", REMARKS, RECORD_STATUS, CREATED_BY, CREATED_WHEN)"
                    + " VALUES(@PosTypeSeq, @EmpTypeSeq, @ClassName, @ClassNameShort, @ClassNameEng, @ClassNameEngShort"
                        + ", @Remarks, @RecordStatus, @CreatedBy, @CreatedWhen)";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.CloseReader();
                dbManager.Dispose();
            }
        }

        public void Update(ClassInfo Info)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(10);

                dbManager.AddParameters(0, "@ClassSeq", Info.Sequence);
                dbManager.AddParameters(1, "@PosTypeSeq", Info.PositionTypes == null ? (int?)null : Info.PositionTypes.Sequence);
                dbManager.AddParameters(2, "@EmpTypeSeq", Info.EmployeeTypes == null ? (int?)null : Info.EmployeeTypes.Sequence);
                dbManager.AddParameters(3, "@ClassName", Info.Name);
                dbManager.AddParameters(4, "@ClassNameShort", Info.NameShort);
                dbManager.AddParameters(5, "@ClassNameEng", Info.NameEng);
                dbManager.AddParameters(6, "@ClassNameEngShort", Info.NameEngShort);
                dbManager.AddParameters(7, "@Remarks", Info.Remarks);
                dbManager.AddParameters(8, "@UpdatedBy", Info.UpdatedBy);
                dbManager.AddParameters(9, "@UpdatedWhen", DateTime.Today);

                strSQL = "UPDATE HRM_MP_CLASSES"
                           + " SET POS_TYPE_SEQ = @PosTypeSeq"
                                + ", EMP_TYPE_SEQ = @EmpTypeSeq"
                                + ", CLASS_NAME = @ClassName"
                                + ", CLASS_NAME_SHORT = @ClassNameShort"
                                + ", CLASS_ENG = @ClassNameEng"
                                + ", CLASS_ENG_SHORT = @ClassNameEngShort"
                                + ", REMARKS = @Remarks"
                               // + ", RECORD_STATUS = @RecordStatus"
                                + ", UPDATED_BY = @UpdatedBy"
                                + ", UPDATED_WHEN = @UpdatedWhen"
                           + " WHERE CLASS_SEQ = @ClassSeq";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.CloseReader();
                dbManager.Dispose();
            }
        }

        public void Delete(int Sequence, int UpdatedBy)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(4);

                dbManager.AddParameters(0, "@Seq", Sequence);
                dbManager.AddParameters(1, "@RecordStatus", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(2, "@UpdatedBy", UpdatedBy);
                dbManager.AddParameters(3, "@UpdatedWhen", DateTime.Today);

                strSQL = "UPDATE HRM_MP_CLASSES"
                    + " SET RECORD_STATUS = @RecordStatus"
                        + ", UPDATED_BY = @UpdatedBy"
                        + ", UPDATED_WHEN = @UpdatedWhen"
                    + " WHERE CLASS_SEQ = @Seq";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.CloseReader();
                dbManager.Dispose();
            }
        }
    }
}
